﻿package dut.ir.lxb.weibo.db;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Timestamp;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;

import dut.ir.lxb.weibo.bean.UserBean;
import dut.ir.lxb.weibo.bean.UserWeiboAction;
import dut.ir.lxb.weibo.bean.WeiboBean;
import dut.ir.lxb.weibo.util.ActionType;

public class WeiboInfoDao
{
	
	private static WeiboInfoDao wd;
	
	private WeiboInfoDao()
	{
		
	}
	
	public static WeiboInfoDao getInstance()
	{
		if(wd == null)
		{
			wd = new WeiboInfoDao();
			return wd;
		}
		else
			return wd;
	}
	
	public boolean hasRecord(WeiboBean wb)
	{
		WeiboBean checkWb = getWeiboByMid(wb.getMid());
		if(checkWb == null)
			return false;
		return true;
	}
	
	public boolean deleteWeiboByMid(String mid)
	{
		DBConnectionManager connectionMan = DBConnectionManager.getInstance();
		Connection conn = null;
		try
		{
			conn = connectionMan.getConnection();
			String sqlStr = "delete from T_WeiboInfo where mid=?";
			PreparedStatement ps = conn.prepareStatement(sqlStr);
			
			//填充参数
			ps.setString(1, mid);
			
			boolean flag = ps.execute();
			ps.close();
			return flag;
		}
		catch (SQLException e)
		{
			// TODO Auto-generated catch block
			e.printStackTrace();
			return false;
		}
		catch(Exception e)
		{
			e.printStackTrace();
			return false;
		}
		finally
		{
			connectionMan.freeConnection(conn);
		}
		
		
	}
	
	public boolean insertWeibo(WeiboBean wb)
	{
		if(wb == null)
			return false;
		if(hasRecord(wb))
			deleteWeiboByMid(wb.getMid());
		//首先从数据库连接池中获得连接
		DBConnectionManager connectionMan = DBConnectionManager.getInstance();
		Connection conn = null;
		try
		{
			conn = connectionMan.getConnection();
			String sqlStr = "insert into T_WeiboInfo (mid,uid,nickname,content,mapdata,likenum,forwardnum,commentnum,posttime,postfrom,url,isforward,forwarduid,forwardmid,forwardnickname,forwardcontent,forwardfrom,eventname,downloadtime) values(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)";
			PreparedStatement ps = conn.prepareStatement(sqlStr);
			
			//填充参数
			ps.setString(1, wb.getMid());
			ps.setString(2, wb.getUid());
			ps.setString(3, wb.getNickname());
			ps.setString(4, wb.getContent());
			ps.setString(5, wb.getMapData());
			ps.setInt(6, wb.getLikeNum());
			ps.setInt(7, wb.getForwardNum());
			ps.setInt(8, wb.getCommentNum());
			
			SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm");
			try
			{
				java.util.Date date = df.parse(wb.getTime());
				Timestamp ts = new Timestamp(date.getTime());
				ps.setTimestamp(9, ts);
			} 
			catch (ParseException e)
			{
				ps.setTimestamp(9, null);
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			catch (Exception e)
			{
				ps.setTimestamp(9, null);
				e.printStackTrace();
			}
			ps.setString(10, wb.getFrom());
			ps.setString(11, wb.getUrl());
			ps.setBoolean(12, wb.isforward());
			ps.setString(13, wb.getForwardUid());
			ps.setString(14, wb.getForwardMid());
			ps.setString(15, wb.getForwardNickname());
			ps.setString(16, wb.getForwardContent());
			ps.setString(17, wb.getForwardFrom());
			ps.setString(18, wb.getEventName());
			
			java.util.Date currentDate = new java.util.Date();
			Timestamp currentTime = new Timestamp(currentDate.getTime());
			ps.setTimestamp(19, currentTime);
			
			boolean flag = ps.execute();
			ps.close();
			return flag;
			
		}
		catch (SQLException e)
		{
			System.err.println(String.format("用户[%s]的微博[%s]插入数据库失败，内容为[%s], 错误信息为[%s]", wb.getNickname(), wb.getMid(), wb.getContent(), e.getMessage()));
			// TODO Auto-generated catch block
			//e.printStackTrace();
			//System.out.println(wb.getContent());
			//System.out.println(wb.getContent().length());
			//System.out.println(wb.getContent().getBytes().length);
			//for(Character c : wb.getContent().toCharArray())
			//	System.out.println(c + " " + c.charValue());
			return false;
		}
		catch(Exception e)
		{
//			e.printStackTrace();
			System.err.println(String.format("用户[%s]的微博[%s]插入数据库失败，内容为[%s], 错误信息为[%s]", wb.getNickname(), wb.getMid(), wb.getContent(), e.getMessage()));
			return false;
		}
		finally
		{
			//释放连接
			connectionMan.freeConnection(conn);
		}
		
	}
	
	
	public void insertWeiboList(List<WeiboBean> weiboList)
	{
		if(weiboList == null)
			return;
		for(WeiboBean weibo : weiboList)
		{
			insertWeibo(weibo);
		}
		
	}
	
	
	public boolean insertEventWeibo(String event, String mid)
	{
		//首先从数据库连接池中获得连接
		DBConnectionManager connectionMan = DBConnectionManager.getInstance();
		Connection conn = null;
		try
		{
			conn = connectionMan.getConnection();
			String sqlStr = "insert into T_Event_Weibo (event,mid) values(?,?)";
			PreparedStatement ps = conn.prepareStatement(sqlStr);
			
			ps.setString(1, event);
			ps.setString(2, mid);
			
			boolean flag = ps.execute();
			ps.close();
			return flag;
		}
		catch (SQLException e)
		{
			// TODO Auto-generated catch block
			e.printStackTrace();
			return false;
		}
		catch(Exception e)
		{
			e.printStackTrace();
			return false;
		}
		finally
		{
			//释放连接
			connectionMan.freeConnection(conn);
		}
	}
	
	/**
	 * 获得用户之间交互的微博
	 * @param user1
	 * @param user2
	 * @param actionType
	 * @param isIncreasing
	 * @return
	 */
	public List<WeiboBean> getUserInteractionWeibo(String user1, String user2, ActionType actionType, boolean isIncreasing)
	{
		//首先从数据库连接池中获得连接
		DBConnectionManager connectionMan = DBConnectionManager.getInstance();
		Connection conn = null;
		try
		{
			String orderCondition = "desc";
			if(isIncreasing)
				orderCondition = "asc";
			String orderStr = "order by posttime " + orderCondition;
			conn = connectionMan.getConnection();
			String tableName = "";
			if(actionType.equals(ActionType.LIKE))
				tableName = "T_Like";
			else if(actionType.equals(ActionType.COMMENT))
			{
				tableName = "T_Comment";
			}
			else if(actionType.equals(ActionType.FORWARD))
			{
				tableName = "T_Forward";
			}
			String sqlStr = String.format("select T_WeiboInfo.mid as newmid,uid,nickname,T_WeiboInfo.content as newcontent,mapdata,likenum,forwardnum,commentnum,posttime,postfrom,url,isforward,T_WeiboInfo.forwarduid,T_WeiboInfo.forwardmid,T_WeiboInfo.forwardnickname,T_WeiboInfo.forwardcontent,T_WeiboInfo.forwardfrom,T_WeiboInfo.eventname from %s,T_WeiboInfo where T_WeiboInfo.mid=%s.mid and T_WeiboInfo.nickname=? and %s.%snickname=?" + orderStr, tableName, tableName, tableName, actionType.toString().toLowerCase());
			PreparedStatement ps = conn.prepareStatement(sqlStr);
			ps.setString(1, user1);
			ps.setString(2, user2);
			List<WeiboBean> wbList = new ArrayList<WeiboBean>();
			
			ResultSet rs = ps.executeQuery();
			while(rs.next())
			{
				WeiboBean wb = new WeiboBean();
				//取出查询数据
				wb.setMid(rs.getString("newmid"));
				wb.setUid(rs.getString("uid"));
				wb.setNickname(rs.getString("nickname"));
				wb.setContent(rs.getString("newcontent"));
				wb.setMapData(rs.getString("mapdata"));
				wb.setLikeNum(rs.getInt("likenum"));
				wb.setForwardNum(rs.getInt("forwardnum"));
				wb.setCommentNum(rs.getInt("commentnum"));
				SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm");
				String time = "";
				if(rs.getTimestamp("posttime") != null)
					time = df.format(rs.getTimestamp("posttime"));
				wb.setTime(time);
				wb.setTime(df.format(rs.getTimestamp("posttime")));
				wb.setFrom(rs.getString("postfrom"));
				wb.setUrl(rs.getString("url"));
				wb.setIsforward(rs.getBoolean("isforward"));
				wb.setForwardUid(rs.getString("forwarduid"));
				wb.setForwardMid(rs.getString("forwardmid"));
				wb.setForwardNickname(rs.getString("forwardnickname"));
				wb.setForwardContent(rs.getString("forwardcontent"));
				wb.setForwardFrom(rs.getString("forwardfrom"));
				wb.setEventName(rs.getString("eventname"));
				
				wbList.add(wb);
			}
			
			rs.close();
			ps.close();
			//对微博按时间排序
			//Collections.sort(wbList, new WeiboComparator());
			return wbList;
		} 
		catch (SQLException e)
		{
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		catch (Exception e)
		{
			e.printStackTrace();
		}
		finally
		{
			//释放连接
			connectionMan.freeConnection(conn);
		}
		return null;
	}
	
	/**
	 * 通过事件获得微博列表
	 * @param eventName 事件名
	 * @param isIncreasing 是否升序
	 * @param isOriginalOnly 是否只有原始微博
	 * @return 微博列表
	 */
	public List<WeiboBean> getWeiboListByEvent(String eventName, boolean isIncreasing, boolean isOriginalOnly)
	{
		//首先从数据库连接池中获得连接
		DBConnectionManager connectionMan = DBConnectionManager.getInstance();
		Connection conn = null;
		try
		{
			
			String originalCondition = "";
			if(isOriginalOnly)
				originalCondition = "and isforward=false";
			conn = connectionMan.getConnection();
			String order = "asc";
			if(!isIncreasing)
				order = "desc";
			String sqlStr = "select T_WeiboInfo.mid as newmid,uid,nickname,content,mapdata,likenum,forwardnum,commentnum,posttime,postfrom,url,isforward,forwarduid,forwardmid,forwardnickname,forwardcontent,forwardfrom,eventname from T_WeiboInfo where eventname=? %s order by posttime %s;";
			sqlStr = String.format(sqlStr, originalCondition, order);
			PreparedStatement ps = conn.prepareStatement(sqlStr);
			ps.setString(1, eventName);
			List<WeiboBean> wbList = new ArrayList<WeiboBean>();
			
			ResultSet rs = ps.executeQuery();
			while(rs.next())
			{
				WeiboBean wb = new WeiboBean();
				//取出查询数据
				wb.setMid(rs.getString("newmid"));
				wb.setUid(rs.getString("uid"));
				wb.setNickname(rs.getString("nickname"));
				wb.setContent(rs.getString("content"));
				wb.setMapData(rs.getString("mapdata"));
				wb.setLikeNum(rs.getInt("likenum"));
				wb.setForwardNum(rs.getInt("forwardnum"));
				wb.setCommentNum(rs.getInt("commentnum"));
				SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm");
				String time = "";
				if(rs.getTimestamp("posttime") != null)
					time = df.format(rs.getTimestamp("posttime"));
				wb.setTime(time);
				wb.setTime(df.format(rs.getTimestamp("posttime")));
				wb.setFrom(rs.getString("postfrom"));
				wb.setUrl(rs.getString("url"));
				wb.setIsforward(rs.getBoolean("isforward"));
				wb.setForwardUid(rs.getString("forwarduid"));
				wb.setForwardMid(rs.getString("forwardmid"));
				wb.setForwardNickname(rs.getString("forwardnickname"));
				wb.setForwardContent(rs.getString("forwardcontent"));
				wb.setForwardFrom(rs.getString("forwardfrom"));
				wb.setEventName(rs.getString("eventname"));
				
				wbList.add(wb);
			}
			
			rs.close();
			ps.close();
			
			//对微博按时间排序
			//Collections.sort(wbList, new WeiboComparator());
			return wbList;
		} 
		catch (SQLException e)
		{
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		catch (Exception e)
		{
			e.printStackTrace();
		}
		finally
		{
			//释放连接
			connectionMan.freeConnection(conn);
		}
		return null;
		
	}
	
	
	
	public WeiboBean getWeiboByMid(String mid)
	{
		//首先从数据库连接池中获得连接
		DBConnectionManager connectionMan = DBConnectionManager.getInstance();
		Connection conn = null;
		try
		{
			conn = connectionMan.getConnection();
			String sqlStr = "select * from T_WeiboInfo where mid=?";
			PreparedStatement ps = conn.prepareStatement(sqlStr);
			ps.setString(1, mid);
			
			ResultSet rs = ps.executeQuery();
			if(rs.next())
			{
				WeiboBean wb = new WeiboBean();
				//取出查询数据
				wb.setMid(rs.getString("mid"));
				wb.setUid(rs.getString("uid"));
				wb.setNickname(rs.getString("nickname"));
				wb.setContent(rs.getString("content"));
				wb.setMapData(rs.getString("mapdata"));
				wb.setLikeNum(rs.getInt("likenum"));
				wb.setForwardNum(rs.getInt("forwardnum"));
				wb.setCommentNum(rs.getInt("commentnum"));
				SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm");
				String time = "";
				if(rs.getTimestamp("posttime") != null)
					time = df.format(rs.getTimestamp("posttime"));
				wb.setTime(time);
				wb.setTime(df.format(rs.getTimestamp("posttime")));
				wb.setFrom(rs.getString("postfrom"));
				wb.setUrl(rs.getString("url"));
				wb.setIsforward(rs.getBoolean("isforward"));
				wb.setForwardUid(rs.getString("forwarduid"));
				wb.setForwardMid(rs.getString("forwardmid"));
				wb.setForwardNickname(rs.getString("forwardnickname"));
				wb.setForwardContent(rs.getString("forwardcontent"));
				wb.setForwardFrom(rs.getString("forwardfrom"));
				wb.setEventName(rs.getString("eventname"));
				
				rs.close();
				ps.close();
				return wb;
			}
			
			else
			{
				return null;
			}
			
		} 
		catch (SQLException e)
		{
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		catch (Exception e)
		{
			e.printStackTrace();
		}
		finally
		{
			//释放连接
			connectionMan.freeConnection(conn);
		}
		
		return null;
	}
	
	/**
	 * 通过UID获得此用户发表的微博列表
	 * @param uid 用户ID
	 * @param isIncreasing 是否升序
	 * @return 微博列表
	 */
	public List<WeiboBean> getWeiboListByUid(String uid, boolean isIncreasing)
	{
		//首先从数据库连接池中获得连接
		DBConnectionManager connectionMan = DBConnectionManager.getInstance();
		Connection conn = null;
		try
		{
			String order = "asc";
			if(!isIncreasing)
				order = "desc";
			
			conn = connectionMan.getConnection();
			String sqlStr = "select * from T_WeiboInfo where uid=? order by posttime %s";
			sqlStr = String.format(sqlStr, order);
			PreparedStatement ps = conn.prepareStatement(sqlStr);
			ps.setString(1, uid);
			List<WeiboBean> wbList = new ArrayList<WeiboBean>();
			
			ResultSet rs = ps.executeQuery();
			while(rs.next())
			{
				WeiboBean wb = new WeiboBean();
				//取出查询数据
				wb.setMid(rs.getString("mid"));
				wb.setUid(rs.getString("uid"));
				wb.setNickname(rs.getString("nickname"));
				wb.setContent(rs.getString("content"));
				wb.setMapData(rs.getString("mapdata"));
				wb.setLikeNum(rs.getInt("likenum"));
				wb.setForwardNum(rs.getInt("forwardnum"));
				wb.setCommentNum(rs.getInt("commentnum"));
				SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm");
				String time = "";
				if(rs.getTimestamp("posttime") != null)
					time = df.format(rs.getTimestamp("posttime"));
				wb.setTime(time);
				wb.setTime(df.format(rs.getTimestamp("posttime")));
				wb.setFrom(rs.getString("postfrom"));
				wb.setUrl(rs.getString("url"));
				wb.setIsforward(rs.getBoolean("isforward"));
				wb.setForwardUid(rs.getString("forwarduid"));
				wb.setForwardMid(rs.getString("forwardmid"));
				wb.setForwardNickname(rs.getString("forwardnickname"));
				wb.setForwardContent(rs.getString("forwardcontent"));
				wb.setForwardFrom(rs.getString("forwardfrom"));
				wb.setEventName(rs.getString("eventname"));
				
				wbList.add(wb);
			}
			
			rs.close();
			ps.close();
			//对微博按时间排序
			//Collections.sort(wbList, new WeiboComparator());
			return wbList;
		} 
		catch (SQLException e)
		{
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		catch (Exception e)
		{
			e.printStackTrace();
		}
		finally
		{
			//释放连接
			connectionMan.freeConnection(conn);
		}
		
		return null;
	}
	
	/**
	 * 通过UID获得此用户发表的微博列表
	 * @param uid 用户ID
	 * @param isIncreasing 是否升序
	 * @param k TopK
	 * @return 微博列表
	 */
	public List<WeiboBean> getTopkWeiboListByUid(String uid, boolean isIncreasing, int k)
	{
		//首先从数据库连接池中获得连接
		DBConnectionManager connectionMan = DBConnectionManager.getInstance();
		Connection conn = null;
		try
		{
			String order = "asc";
			if(!isIncreasing)
				order = "desc";
			
			conn = connectionMan.getConnection();
			String sqlStr = "select * from T_WeiboInfo where uid=? order by posttime %s limit ?";
			sqlStr = String.format(sqlStr, order);
			PreparedStatement ps = conn.prepareStatement(sqlStr);
			ps.setString(1, uid);
			ps.setInt(2, k);
			List<WeiboBean> wbList = new ArrayList<WeiboBean>();
			
			ResultSet rs = ps.executeQuery();
			while(rs.next())
			{
				WeiboBean wb = new WeiboBean();
				//取出查询数据
				wb.setMid(rs.getString("mid"));
				wb.setUid(rs.getString("uid"));
				wb.setNickname(rs.getString("nickname"));
				wb.setContent(rs.getString("content"));
				wb.setMapData(rs.getString("mapdata"));
				wb.setLikeNum(rs.getInt("likenum"));
				wb.setForwardNum(rs.getInt("forwardnum"));
				wb.setCommentNum(rs.getInt("commentnum"));
				SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm");
				String time = "";
				if(rs.getTimestamp("posttime") != null)
					time = df.format(rs.getTimestamp("posttime"));
				wb.setTime(time);
				wb.setTime(df.format(rs.getTimestamp("posttime")));
				wb.setFrom(rs.getString("postfrom"));
				wb.setUrl(rs.getString("url"));
				wb.setIsforward(rs.getBoolean("isforward"));
				wb.setForwardUid(rs.getString("forwarduid"));
				wb.setForwardMid(rs.getString("forwardmid"));
				wb.setForwardNickname(rs.getString("forwardnickname"));
				wb.setForwardContent(rs.getString("forwardcontent"));
				wb.setForwardFrom(rs.getString("forwardfrom"));
				wb.setEventName(rs.getString("eventname"));
				
				wbList.add(wb);
			}
			
			rs.close();
			ps.close();
			//对微博按时间排序
			//Collections.sort(wbList, new WeiboComparator());
			return wbList;
		}
		catch (SQLException e)
		{
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		catch (Exception e)
		{
			e.printStackTrace();
		}
		finally
		{
			//释放连接
			connectionMan.freeConnection(conn);
		}
		
		return null;
	}
	
	/**
	 * 通过UID获得此用户发表的微博数量
	 * @param uid 用户ID
	 * @return 微博数量
	 */
	public int getWeiboCountByUid(String uid)
	{
		//首先从数据库连接池中获得连接
		DBConnectionManager connectionMan = DBConnectionManager.getInstance();
		Connection conn = null;
		try
		{
			
			conn = connectionMan.getConnection();
			String sqlStr = "select count(*) from T_WeiboInfo where uid=?";
			PreparedStatement ps = conn.prepareStatement(sqlStr);
			ps.setString(1, uid);
			List<WeiboBean> wbList = new ArrayList<WeiboBean>();
			
			int weiboCount = 0;
			ResultSet rs = ps.executeQuery();
			while(rs.next())
			{
				weiboCount = rs.getInt(1);
			}
			
			rs.close();
			ps.close();
			//对微博按时间排序
			//Collections.sort(wbList, new WeiboComparator());
			return weiboCount;
		}
		catch (SQLException e)
		{
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		catch (Exception e)
		{
			e.printStackTrace();
		}
		finally
		{
			//释放连接
			connectionMan.freeConnection(conn);
		}
		
		return 0;
	}
	
	
	/**
	 * 通过UID获得此用户发表的微博列表
	 * @param uid
	 * @param isEventOnly
	 * @param timeDesc
	 * @return
	 */
	public List<WeiboBean> getAllWeiboList(boolean isTimeIncreasing)
	{
		String order = "asc";
		if(!isTimeIncreasing)
			order = "desc";
		//首先从数据库连接池中获得连接
		DBConnectionManager connectionMan = DBConnectionManager.getInstance();
		Connection conn = null;
		try
		{
			String eventCondition = "";
			//if(isEventOnly)
			//	eventCondition = "and event is not null";
			conn = connectionMan.getConnection();
			String sqlStr = "select * from T_WeiboInfo where posttime is not null %s order by posttime %s";
			sqlStr = String.format(sqlStr, eventCondition, order);
			
			PreparedStatement ps = conn.prepareStatement(sqlStr);
			List<WeiboBean> wbList = new ArrayList<WeiboBean>();
			
			ResultSet rs = ps.executeQuery();
			while(rs.next())
			{
				WeiboBean wb = new WeiboBean();
				//取出查询数据
				wb.setMid(rs.getString("mid"));
				wb.setUid(rs.getString("uid"));
				wb.setNickname(rs.getString("nickname"));
				wb.setContent(rs.getString("content"));
				wb.setMapData(rs.getString("mapdata"));
				wb.setLikeNum(rs.getInt("likenum"));
				wb.setForwardNum(rs.getInt("forwardnum"));
				wb.setCommentNum(rs.getInt("commentnum"));
				SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm");
				String time = "";
				if(rs.getTimestamp("posttime") != null)
					time = df.format(rs.getTimestamp("posttime"));
				wb.setTime(time);
				wb.setFrom(rs.getString("postfrom"));
				wb.setUrl(rs.getString("url"));
				wb.setIsforward(rs.getBoolean("isforward"));
				wb.setForwardUid(rs.getString("forwarduid"));
				wb.setForwardMid(rs.getString("forwardmid"));
				wb.setForwardNickname(rs.getString("forwardnickname"));
				wb.setForwardContent(rs.getString("forwardcontent"));
				wb.setForwardFrom(rs.getString("forwardfrom"));
				wb.setEventName(rs.getString("eventname"));
				
				wbList.add(wb);
			}
			
			rs.close();
			ps.close();
			
			//对微博按时间排序
			//Collections.sort(wbList, new WeiboComparator());
			return wbList;
		} 
		catch (SQLException e)
		{
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		catch (Exception e)
		{
			e.printStackTrace();
		}
		finally
		{
			//释放连接
			connectionMan.freeConnection(conn);
		}
		
		return null;
	}
	
	
	/**
	 * 选出交互最多的前K条微博
	 * @param uid 用户ID
	 * @param actionType 交互类型，点赞、转发、评论
	 * @param k 前K个
	 * @return 微博列表
	 */
	public List<WeiboBean> getTopkWeiboByActionType(String uid, ActionType actionType, int k)
	{
		//首先从数据库连接池中获得连接
		DBConnectionManager connectionMan = DBConnectionManager.getInstance();
		Connection conn = null;
		try
		{
			String orderColumn;
			if(actionType == ActionType.COMMENT)
				orderColumn = "commentnum";
			else if(actionType == ActionType.LIKE)
				orderColumn = "likenum";
			else
				orderColumn = "forwardnum";
			
			conn = connectionMan.getConnection();
			String sqlStr = "select * from T_WeiboInfo where uid=? order by %s desc limit ?";
			sqlStr = String.format(sqlStr, orderColumn);
			PreparedStatement ps = conn.prepareStatement(sqlStr);
			ps.setString(1, uid);
			ps.setInt(2, k);
			List<WeiboBean> wbList = new ArrayList<WeiboBean>();
			
			ResultSet rs = ps.executeQuery();
			while(rs.next())
			{
				WeiboBean wb = new WeiboBean();
				//取出查询数据
				wb.setMid(rs.getString("mid"));
				wb.setUid(rs.getString("uid"));
				wb.setNickname(rs.getString("nickname"));
				wb.setContent(rs.getString("content"));
				wb.setMapData(rs.getString("mapdata"));
				wb.setLikeNum(rs.getInt("likenum"));
				wb.setForwardNum(rs.getInt("forwardnum"));
				wb.setCommentNum(rs.getInt("commentnum"));
				SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm");
				String time = "";
				if(rs.getTimestamp("posttime") != null)
					time = df.format(rs.getTimestamp("posttime"));
				wb.setTime(time);
				wb.setTime(df.format(rs.getTimestamp("posttime")));
				wb.setFrom(rs.getString("postfrom"));
				wb.setUrl(rs.getString("url"));
				wb.setIsforward(rs.getBoolean("isforward"));
				wb.setForwardUid(rs.getString("forwarduid"));
				wb.setForwardMid(rs.getString("forwardmid"));
				wb.setForwardNickname(rs.getString("forwardnickname"));
				wb.setForwardContent(rs.getString("forwardcontent"));
				wb.setForwardFrom(rs.getString("forwardfrom"));
				wb.setEventName(rs.getString("eventname"));
				
				wbList.add(wb);
			}
			
			rs.close();
			ps.close();
			//对微博按时间排序
			//Collections.sort(wbList, new WeiboComparator());
			return wbList;
		} 
		catch (SQLException e)
		{
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		catch (Exception e)
		{
			e.printStackTrace();
		}
		finally
		{
			//释放连接
			connectionMan.freeConnection(conn);
		}
		
		return null;				
	}
	
	
	/**
	 * 获得参与某一事件的所有用户
	 * @param eventName 事件名
	 * @return 参与事件的用户列表
	 */
	public List<UserBean> getActiveUserByEventname(String eventName)
	{
		List<UserBean> userList = new ArrayList<UserBean>();
		
		UserWeiboActionDao actionDao = UserWeiboActionDao.getInstance();
		UserInfoDao userDao = UserInfoDao.getInstance();
		List<WeiboBean> weiboList = this.getWeiboListByEvent(eventName, false, false);
		for(WeiboBean weibo : weiboList)
		{
			userList.add(userDao.getUserByUid(weibo.getUid()));
			
			List<UserWeiboAction> actionList = actionDao.getForwardListByMid(weibo.getMid());
			for(UserWeiboAction action : actionList)
			{
				userList.add(userDao.getUserByUid(action.getUid()));
			}
		}
		
		return userList;
		
	}
	
	/**
	 * 获得某个固定日期之后的微博，便于新增索引
	 * @param timeStamp 时间点
	 * @param isTimeIncreasing
	 * @return 躲雨列表
	 */
	public List<WeiboBean> getNewWeiboList(String timeStamp, boolean isTimeIncreasing)
	{
		String order = "asc";
		if(!isTimeIncreasing)
			order = "desc";
		//首先从数据库连接池中获得连接
		DBConnectionManager connectionMan = DBConnectionManager.getInstance();
		Connection conn = null;
		try
		{
			String eventCondition = "";
			//if(isEventOnly)
			//	eventCondition = "and event is not null";
			conn = connectionMan.getConnection();
			String sqlStr = "select * from T_WeiboInfo where posttime is not null %s and unix_timestamp(downloadtime) > unix_timestamp(?) order by posttime %s";
			sqlStr = String.format(sqlStr, eventCondition, order);
			
			PreparedStatement ps = conn.prepareStatement(sqlStr);
			ps.setString(1, timeStamp);
			List<WeiboBean> wbList = new ArrayList<WeiboBean>();
			
			ResultSet rs = ps.executeQuery();
			while(rs.next())
			{
				WeiboBean wb = new WeiboBean();
				//取出查询数据
				wb.setMid(rs.getString("mid"));
				wb.setUid(rs.getString("uid"));
				wb.setNickname(rs.getString("nickname"));
				wb.setContent(rs.getString("content"));
				wb.setMapData(rs.getString("mapdata"));
				wb.setLikeNum(rs.getInt("likenum"));
				wb.setForwardNum(rs.getInt("forwardnum"));
				wb.setCommentNum(rs.getInt("commentnum"));
				SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm");
				String time = "";
				if(rs.getTimestamp("posttime") != null)
					time = df.format(rs.getTimestamp("posttime"));
				wb.setTime(time);
				wb.setFrom(rs.getString("postfrom"));
				wb.setUrl(rs.getString("url"));
				wb.setIsforward(rs.getBoolean("isforward"));
				wb.setForwardUid(rs.getString("forwarduid"));
				wb.setForwardMid(rs.getString("forwardmid"));
				wb.setForwardNickname(rs.getString("forwardnickname"));
				wb.setForwardContent(rs.getString("forwardcontent"));
				wb.setForwardFrom(rs.getString("forwardfrom"));
				wb.setEventName(rs.getString("eventname"));
				
				wbList.add(wb);
			}
			
			rs.close();
			ps.close();
			
			//对微博按时间排序
			//Collections.sort(wbList, new WeiboComparator());
			return wbList;
		} 
		catch (SQLException e)
		{
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		catch (Exception e)
		{
			e.printStackTrace();
		}
		finally
		{
			//释放连接
			connectionMan.freeConnection(conn);
		}
		
		return null;
	}
	
	public static void main(String [] args)
	{
		WeiboInfoDao weiboDao = WeiboInfoDao.getInstance();
		System.out.println(weiboDao.getWeiboByMid("3965606874984858"));
		
	}
	
	
}
